package jdom;
import java.util.ArrayList;

public class Utils {

	/**
	 * Check whether a Library is already in a list of libraries. Libraries are
	 * equal if all their information are equivalent.
	 * 
	 * @param l
	 * @param list
	 * @return true if contained
	 */
	public static boolean isAlreadyContained(Library l, ArrayList<Library> list) {

		for (Library lib : list) {
			if (lib.getName().equals(l.getName())) {
				if (lib.getVersion().equals(l.getVersion())) {
					if (lib.getLanguage().equals(l.getLanguage())) {
						if (lib.getSource().equals(l.getSource())) {
							if (Utils.areArrayListsEqual(lib.getDevelopers(),
									l.getDevelopers())) {
								if (Utils.areArrayListsEqual(
										lib.getDependencies(),
										l.getDependencies())) {
									return true;
								}
							}
						}
					}
				}
			}
		}
		return false;
	}

	/**
	 * Check whether a Developer is already in a list of developers. Devel-
	 * opers are equal if they are equally named,
	 * 
	 * @param d
	 * @param list
	 * @return true if contained
	 */
	public static boolean isAlreadyContained(Developer d,
			ArrayList<Developer> list) {
		for (Developer dev : list) {
			if (dev.getName().equals(d.getName())) {
				return true;
			}
		}
		return false;
	}

	public static boolean areArrayListsEqual(ArrayList<String> arrayList1,
			ArrayList<String> arrayList2) {
		for (String elementOfarrayList1 : arrayList1) {
			if (!arrayList2.contains(elementOfarrayList1)) {
				return false;
			}
		}
		return true;
	}

}
